//
//  LoginController.m
//  youthNote
//
//  Created by huangrensheng on 15/12/31.
//  Copyright © 2015年 laber. All rights reserved.
//

typedef NS_ENUM(NSInteger) {
    LoginBtnTypeLogin = 1,
    LoginBtnTypeRegister = 2,
    LoginBtnTypeFindPass = 3
}LoginBtnType;

#import "LoginController.h"
#import "RegasterController.h"

@interface LoginController ()

@property (nonatomic, strong) UITextField *userName;

@property (nonatomic, strong) UITextField *passWord;

@property (nonatomic, strong) UIButton *registerBtn;

@property (nonatomic, strong) UIButton *loginBtn;

@property (nonatomic, strong) UIButton *finPass;//找回密码

@end

@implementation LoginController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createWithLeftStyle:@{@"type":@"back"} middleStyle:@{@"type":@"title",@"title":@"登录"} rightStyle:nil];
    [self initChildViews];
    [self makeConstraints];
}

- (void)initChildViews{
    self.userName = [self createTextFiled:@"login_key" plach:@"用户名"];
    [self.view addSubview:self.userName];
    
    self.passWord = [self createTextFiled:@"login_user" plach:@"密码"];
    self.passWord.secureTextEntry = YES;
    [self.view addSubview:self.passWord];
    
    self.loginBtn = [[UIButton alloc] initWithIcon:nil title:@"登录" textClolor:[UIColor whiteColor] textFont:18];
    [self.loginBtn addTarget:self action:@selector(didBtnOnClick:) forControlEvents:UIControlEventTouchUpInside];
    self.loginBtn.tag = LoginBtnTypeLogin;
    [self.loginBtn setBackgroundImage:[UIImage imageNamed:@"reigster_btn"] forState:UIControlStateNormal];
    [self.loginBtn setBackgroundImage:[UIImage imageNamed:@"reigster_btn_select"] forState:UIControlStateHighlighted];
    [self.view addSubview:self.loginBtn];
    
    self.registerBtn = [self addUnderLineText:@"免费注册"];
    [self.registerBtn addTarget:self action:@selector(didBtnOnClick:) forControlEvents:UIControlEventTouchUpInside];
    self.registerBtn.tag = LoginBtnTypeRegister;
    [self.view addSubview:self.registerBtn];
    
    self.finPass = [self addUnderLineText:@"忘记密码?"];
    [self.finPass addTarget:self action:@selector(didBtnOnClick:) forControlEvents:UIControlEventTouchUpInside];
    self.finPass.tag = LoginBtnTypeFindPass;
    [self.view addSubview:self.finPass];
    
}

- (void)didBtnOnClick:(UIButton *)btn{
    if (btn.tag == LoginBtnTypeLogin) {
        [[HttpManger shareHttpManage] postWithUrl:@"login" params:@{@"userName":self.userName.text,@"passWord":self.passWord.text} success:^(NSDictionary *data) {
            [[NSUserDefaults standardUserDefaults] setValue:data forKey:@"token"];
            [self.navigationController popViewControllerAnimated:YES];
        } error:^(NSError *error) {
            
        }];
    }else if(btn.tag == LoginBtnTypeRegister){
        RegasterController *registe = [[RegasterController alloc] init];
        [self.navigationController pushViewController:registe animated:YES];
    }else{
    
    }
}

- (void)makeConstraints{
    __weak typeof(self) weakSelf = self;
    [self.userName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(weakSelf.view.mas_leading).equalTo(inset(15));
        make.top.equalTo(weakSelf.view.mas_top).equalTo(inset(20));
        make.trailing.equalTo(weakSelf.view.mas_trailing).equalTo(inset(-15));
        make.height.equalTo(inset(44));
    }];
    
    [self.passWord mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(weakSelf.view.mas_leading).equalTo(inset(15));
        make.top.equalTo(weakSelf.userName.mas_bottom).equalTo(inset(15));
        make.trailing.equalTo(weakSelf.view.mas_trailing).equalTo(inset(-15));
        make.height.equalTo(inset(44));
    }];
    
    [self.loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(weakSelf.view.mas_leading).equalTo(inset(15));
        make.top.equalTo(weakSelf.passWord.mas_bottom).equalTo(inset(20));
        make.trailing.equalTo(weakSelf.view.mas_trailing).equalTo(inset(-15));
        make.height.equalTo(inset(44));
    }];
    
    [self.registerBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(weakSelf.view.mas_leading).equalTo(inset(15));
        make.top.equalTo(weakSelf.loginBtn.mas_bottom);
        make.height.equalTo(inset(44));
    }];
    
    [self.finPass mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.loginBtn.mas_bottom);
        make.trailing.equalTo(weakSelf.view.mas_trailing).equalTo(inset(-15));
        make.height.equalTo(inset(44));
    }];
}

#pragma mark - 添加下划线
- (UIButton *)addUnderLineText:(NSString *)titleText{
    UIButton *btn = [[UIButton alloc] initWithIcon:nil title:titleText textClolor:BAR_COLOR textFont:14];
    NSMutableAttributedString *title = [[NSMutableAttributedString alloc] initWithString:titleText];
    NSRange titleRange = {0,[title length]};
    [title addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:titleRange];
    [title addAttributes:[NSDictionary dictionaryWithObjectsAndKeys:BAR_COLOR,NSForegroundColorAttributeName, nil] range:titleRange];
    [btn setAttributedTitle:title forState:UIControlStateNormal];
    
    return btn;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
